// miniloop, a simple live looper
//
// Copyright (C) 2007 Nick Thomas
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the <organization> nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY NICK THOMAS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL NICK THOMAS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <stdio.h>
#include <stdlib.h>
#include "Config.hpp"
using namespace miniloop;

Config miniloop::g_config;

Config::Config()
{
    num_loops = 12;
    loop_dir = Glib::ustring(getenv("HOME")) + "/loops";
    loop_length = 418873;
    num_bars = 4;
    beats_per_bar = 4;

    loop_width = 100;
    loop_height = 100;
    loop_rows = 2;
    loop_cols = 6;

    midi_channel = 0;
    midi_pitch_control = 16;
    midi_hold_control = 17;

    src_converter_type = 3;
    src_pitch_range = 0.08;
}

void Config::load_config_file()
{
    Glib::ustring config_file = Glib::ustring(getenv("HOME")) + "/.miniloop";

    FILE *file = fopen(config_file.c_str(), "r");

    if (!file) {
	printf("Warning: config file %s not found\n", config_file.c_str());
	return;
    }

    char var[32];
    char value[1024];
    int status;

    while (1) {
	status = fscanf(file, " %31s = %1023s ", var, value);

	if (status == EOF) {
	    break;
	} else if (status < 2) {
	    printf("Warning: syntax error in config file.\n");
	} else {
	    set_config_var(var, value);
	}
    }
}

void Config::set_config_var(Glib::ustring var, Glib::ustring value)
{
    if (var == "num_loops") {
	num_loops = atoi(value.c_str());
    } else if (var == "loop_dir") {
	loop_dir = value;
    } else if (var == "loop_length") {
	loop_length = atoi(value.c_str());
    } else if (var == "num_bars") {
	num_bars = atoi(value.c_str());
    } else if (var == "beats_per_bar") {
	beats_per_bar = atoi(value.c_str());
    } else if (var == "loop_width") {
	loop_width = atoi(value.c_str());
    } else if (var == "loop_height") {
	loop_height = atoi(value.c_str());
    } else if (var == "loop_rows") {
	loop_rows = atoi(value.c_str());
    } else if (var == "loop_cols") {
	loop_cols = atoi(value.c_str());
    } else if (var == "midi_channel") {
	midi_channel = atoi(value.c_str());
    } else if (var == "midi_pitch_control") {
	midi_pitch_control = atoi(value.c_str());
    } else if (var == "midi_hold_control") {
	midi_hold_control = atoi(value.c_str());
    } else if (var == "src_converter_type") {
	src_converter_type = atoi(value.c_str());
    } else if (var == "src_pitch_range") {
	src_pitch_range = strtod(value.c_str(), NULL);
    }
}
